<?php
include './connectMySQL.php';
include './building.php';

$name = isset($_GET["name"]) ? trim($_GET["name"]) : "";
$postcode = isset($_GET["postcode"]) ? trim($_GET["postcode"]) : "";
$category = isset($_GET["category"]) ? trim($_GET["category"]) : "";

$query = "SELECT * FROM building WHERE building_type='$category'";
if (!empty($name)) {
    $query = $query . " AND name='$name'";
}
if (!empty($postcode)) {
    $query = $query . " AND postcode='$postcode'";
}

$db = new MySQLDatabase(); // create a Database object
$mysqli = $db->connect("infs3202-146", "spice*Dizzy6streak", "infs3202-146");

$result = $mysqli->query($query);

if ($result) {
    $buildingArray = [];
    while ($row = $result->fetch_assoc()) {
        $building = new Building($row);
        $buildingArray[] = $building;
    }
} else {
    die(mysql_error()); // useful for debugging
}
$db->disconnect();
?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form method="GET">
            <label for="name">Name</label>
            <input type="text" name="name" id="name" />

            <label for="postcode">Postcode</label>
            <input type="text" name="postcode" id="postcode" />

            <label for="category">Category</label>
            <select name="category">
                <option value="HOTEL">Hotel</option>
                <option value="HOSTEL">Hostel</option>
            </select> 

            <input type="submit" value="Search" />
        </form>

        <?php
        if (!empty($buildingArray)) {
            $buildingType = $category;
            include './_preview-table.php';
        }
        ?>
    </body>
</html>
